<?php
namespace app\agent\controller;
use \app\admin\model\Agent as AgentModel;
use app\lib\exception\SuccessMessage;
use think\exception\ErrorException;
use think\Request;
use app\lib\validate\AgentAddCheck;



class Agent extends Base{
    public function index() {
        $postData = Request::instance()->post();
        if($postData) {
            $name = Request::instance()->post('agent_name') ? Request::instance()->post('agent_name') : null;
            $mobile = Request::instance()->post('phone') ? Request::instance()->post('phone') : null;

            $seachData['name'] = array('like', "%".$name."%");
            $seachData['mobile'] = array('like', "%".$mobile."%");
            $seachData['status'] = 1;
            $seachData['uper_id'] = session('agentId');
            $list = AgentModel::where($seachData)->select();

            if(count($list) == 0) {
                throw new LoginException([
                    'msg' => '没有找到数据'
                ]);
            }else {
                throw new SuccessMessage([
                    'data' => $list
                ]);
            }
        }else {
            $list = AgentModel::where(array('status' => 1, 'uper_id' => session('agentId')))->select();

        }

        $this->assign('list', $list);
        return view();
    }

    //添加代理商
    public function add() {
        if(Request::instance()->post()) {
            $data = $this->checkAgentAdd();
            $adminDB = new AgentModel;

            //是修改
            if(Request::instance()->post('edit_id')) {
                $id = $data['edit_id'];
                unset($data['edit_id']);
                $ret = $adminDB->where(array('id' => $id))->update($data);
                if($ret){
                    throw new SuccessMessage([
                        'msg' => '更新成功'
                    ]);
                }else {
                    throw new ErrorException([
                        'msg' => '更新失败'.$this->error
                    ]);
                }
            }

            if(count((new AgentModel)->where(array('mobile' => $data['mobile']))->find()) != 0) {
                throw new LoginException([
                    'msg' => '该手机号的用户已注册'
                ]);
            }

            $data['uper_id'] = session("agentId");
            $data['uper_name'] = session("agentName");
            $res = $adminDB->insert($data);
            if($res) {
                $adminDB->where(array('id' => session('agentId')))->setInc('agent_num', 1);
                throw new SuccessMessage([
                    'msg' => '添加代理商成功'
                ]);
            }else {
                throw new ErrorException([
                    'msg' => '添加失败'.$this->error
                ]);
            }
        }

        $this->assign('uper_name', session('agentName'));
        return view();
    }

    //编辑
    public function edit() {
        $id = Request::instance()->param('id');
        $content = AgentModel::where(array('id' => $id))->find();

        $this->assign('uper_name', session('agentName'));
        $this->assign('list', $content);
        return view();
    }

    //删除代理商
    public function del() {
        if(Request::instance()->post()) {
            $id = intval(Request::instance()->post('id'));
            if(AgentModel::where(array('id' => $id))->update(array('status' => 0))) {
                $adminDB = (new AgentModel);
                $uper_id = $adminDB->where(array('id' => $id))->find();
                $adminDB->where(array('id' => $uper_id['uper_id']))->setDec('agent_num', 1);

                throw new SuccessMessage([
                    'msg' => '删除成功'
                ]);
            }else {
                throw new LoginException([
                    'msg' => '删除失败'
                ]);
            }
        }
    }

    //代理商结构图
    public function construct() {
        if(Request::instance()->post()) {
            $data = array(
                'data' => array(
                    'id' => 1,
                    'name' => '第一个节点',
                    'pid' => null,
                    'childrens' => array(
                        array(
                            'id' => 2,
                            'name' => '第2个节点',
                            'pid' => 1,
                            'childrens' => ''
                        ),
                        array(
                            'id' => 3,
                            'name' => '第3个节点',
                            'pid' => 1,
                            'childrens' => array(
                                array(
                                    'id' => 4,
                                    'name' => '第4个节点',
                                    'pid' => 3,
                                    'childrens' => ''
                                ),
                                array(
                                    'id' => 5,
                                    'name' => '第5个节点',
                                    'pid' => 3,
                                    'childrens' => ''
                                )
                            )
                        ),
                        array(
                            'id' => 3,
                            'name' => '第3个节点',
                            'pid' => 1,
                            'childrens' => array(
                                array(
                                    'id' => 6,
                                    'name' => '第4个节点',
                                    'pid' => 3,
                                    'childrens' => ''
                                ),
                                array(
                                    'id' => 5,
                                    'name' => '第5个节点',
                                    'pid' => 3,
                                    'childrens' => ''
                                )
                            )
                        ),
                        array(
                            'id' => 3,
                            'name' => '第3个节点',
                            'pid' => 1,
                            'childrens' => array(
                                array(
                                    'id' => 4,
                                    'name' => '第4个节点',
                                    'pid' => 3,
                                    'childrens' => ''
                                ),
                                array(
                                    'id' => 5,
                                    'name' => '第5个节点',
                                    'pid' => 3,
                                    'childrens' => ''
                                )
                            )
                        ),
                        array(
                            'id' => 3,
                            'name' => '第3个节点',
                            'pid' => 1,
                            'childrens' => array(
                                array(
                                    'id' => 4,
                                    'name' => '第4个节点',
                                    'pid' => 3,
                                    'childrens' => ''
                                ),
                                array(
                                    'id' => 5,
                                    'name' => '第5个节点',
                                    'pid' => 3,
                                    'childrens' => ''
                                )
                            )
                        ),
                        array(
                            'id' => 3,
                            'name' => '第3个节点',
                            'pid' => 1,
                            'childrens' => array(
                                array(
                                    'id' => 4,
                                    'name' => '第4个节点',
                                    'pid' => 3,
                                    'childrens' => ''
                                ),
                                array(
                                    'id' => 5,
                                    'name' => '第5个节点',
                                    'pid' => 3,
                                    'childrens' => ''
                                )
                            )
                        ),
                        array(
                            'id' => 3,
                            'name' => '第3个节点',
                            'pid' => 1,
                            'childrens' => array(
                                array(
                                    'id' => 4,
                                    'name' => '第4个节点',
                                    'pid' => 3,
                                    'childrens' => ''
                                ),
                                array(
                                    'id' => 5,
                                    'name' => '第5个节点',
                                    'pid' => 3,
                                    'childrens' => ''
                                )
                            )
                        ),
                    )
                )
            );

            show(1,'','',$data);
        }
        return view();
    }

    //递归生成树
    function FormatTree($data,$parentId,$sonId, $pid = 0)
    {
        $arr = array();
        $tem = array();
        foreach ($data as $v) {
            if ($v[$parentId] == $pid) {
                $tem = FormatTree($data,$parentId,$sonId,$v[$sonId]);//递归，调用自身
                // 判断是否存在子数组
                $v['son'] = $tem;
            $arr[] = $v;
        }
        }
        return $arr;
    }
}